package com.stockz.equity.charting.builder;

import java.awt.BorderLayout;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stockz.equity.charting.painter.mode.Mode;
import com.stockz.equity.charting.util.SwingUtil;
import com.stockz.equity.charting.util.UIConfigurationUtil;
import com.stockz.equity.charting.view.StockChart;
import com.stockz.equity.charting.view.StockChartPanel;
import com.stockz.equity.charting.view.StockChartPanelSkin;
import com.stockz.equity.charting.view.StockChartPanelStructure;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.manager.ConfigurationManager;
import com.stockz.equity.pattern.Builder;

@Component
public class StockChartViewBuilder implements Builder<StockChartView> {

	@Autowired private ConfigurationManager configurationManager;
	@Autowired private UIConfigurationUtil contextUtil;
	@Autowired private StockChartView stockChartView;
	@Autowired private StockChartPanelSkin stockChartPanelSkin;
	@Autowired private StockChartPanelStructure stockChartPanelStructure;
	
	
	
	@Override
	public StockChartView build() {
		configurationManager.configureBean(stockChartView.getStockChartViewSkin());
		configurationManager.configureBean(stockChartView.getStockChartViewStructure());
		configurationManager.configureBean(stockChartPanelSkin);
		configurationManager.configureBean(stockChartPanelStructure);
		
		contextUtil.configureParentsAndChildren();
		
		for(StockChart stockChart : stockChartView.getStockChartList()){
			StockChartPanel panel = stockChart.getStockChartPanel();
			panel.setBackground(panel.getStockChartPanelSkin().getBackgroundColor());
			
			Mode mode = panel.getStockChartPanelState().getMode();
			panel.addMouseListener(mode.getModeListener());
			panel.addMouseMotionListener(mode.getModeListener());
			panel.addMouseWheelListener(mode.getModeListener());
			panel.addKeyListener(mode.getModeListener());
		}
		
		java.awt.Component component = SwingUtil.split(stockChartView.getStockChartList());
		
		/*JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,false);
		splitPane.setTopComponent(component);
		splitPane.setBottomComponent(stockChartView.getStockChartControlPanel());
		splitPane.setPreferredSize(new Dimension(stockChartView.getVisibleRect().width,stockChartView.getVisibleRect().height));
		stockChartView.add(splitPane,BorderLayout.CENTER);*/
		
	/*stockChartView.getStockChartControlPanel().setPreferredSize(new Dimension(r.width,
				 stockChartView.getStockChartViewStructure().getStockChartControlPanelHeight()));*/
		stockChartView.add(stockChartView.getStockChartControlPanel(),BorderLayout.SOUTH);
		stockChartView.getStockChartControlPanel().setStockChartView(stockChartView);


/*		component.setPreferredSize(new Dimension(r.width,
				r.height - stockChartView.getStockChartViewStructure().getStockChartControlPanelHeight()));*/
		stockChartView.add(component,BorderLayout.CENTER);


		return stockChartView;
	}
}
